/*global _, $ */
/*jshint bitwise:true, curly:true, eqeqeq:true, forin:true, immed:true, latedef:true, newcap:true,
 undef:true, trailing:true, smarttabs:true, sub:true, browser:true, devel:true, maxlen:150 */
////////////////////  biz  ////////////////////
_.ext.ini();
_.action.extend({
	'add-item': function () {
		_.tool.addItem();
	}
});

_.tool = {
	ini: function () {
		this._getElem();
		this._getHTML();
		this._bind();
		this.adjustTextarea(this.jOutput);
	},
	_getElem: function () {
		this.jInputWrapper = $('#dv_input');
		this.jInputForm = this.jInputWrapper.find('.form');
		this.jOutput = $('#ta_output');
		this.jBtnJson = $('#in_s_json');
		this.jBtnExcel = $('#in_s_excel');
		this.jMailLink = $('#dv_footer p a');
	},
	_bind: function () {
		var _ns = this;
		this.jBtnJson.click(function (e) {
			e.preventDefault();
			_ns.convTo('json');
		});
		this.jBtnExcel.click(function (e) {
			e.preventDefault();
			_ns.convTo('excel');
		});
		this.jInputWrapper.on('blur', 'textarea', function () {
			this.value = _ns.formatInput(this.value);
		});
		_.dom.jBody.on('focus', 'textarea', function () {
			this.select();
		});
		this.jMailLink.click(function () {
			var url = this.href;
			url = url.replace('[[[at]]]', '@');
			this.href = url;
			return true;
		});
	},
	_getHTML: function () {
		this.html = this.jInputForm.html();
	},
	addItem: function () {
		this.jInputForm.append(this.html);
	},
	adjustTextarea: function (e) {
		var jThis = $(e);
		var contentHeight = jThis[0].scrollHeight;
		var minHeight = parseFloat(jThis.css('min-height')) || 0;
		if (contentHeight > minHeight - 14) {
			jThis.height(contentHeight + 14);
		}
	},
	formatInput: function (s) {
		//console.log(s);
		s = _.str.trim(s);
		if (_.str.startsWith(s, '[')) {
			s = s.replace(/'/g, '"');  //beautify json
			s = _.str.parseJSON(s).join(' ');
		} else {
			//console.log(s);
			s = s.replace(/,/g, ' ');
			//console.log(s);
			s = s.replace(/　/g, ' ');
			//console.log(s);
			s = s.replace(/，/g, ' ');
			//console.log(s);
			s = s.replace(/、/g, ' ');
			//console.log(s);
			s = s.replace(/\s+/g, ' ');
		}
		s = _.str.trim(s);
		return s;
	},
	convTo: function (type) {
		this._getVal();
		if (!this.input) {
			this.output = '输入为空，请重新输入。';
		} else {
			//console.log(this.input);
			var r = this.input.split(' ');
			//console.log(r);
			r = _.str.uniq(r);
			//console.log(r);
			if (type === 'json') {
				r = _.shuffle(r);
				//console.log(r);
				this.output = JSON.stringify(r);
			} else {
				this.output = r.join('\n');
			}
			//console.log(this.output);
			this._output();
		}
		this._output();
	},
	_getVal: function () {
		var jInput = this.jInputForm.find('textarea');
		var a = [];
		jInput.each(function () {
			//console.log(this.value);
			if (this.value) {a.push(this.value);}
		});
		this.input = _.str.clean(a.join(' '));
	},
	_output: function () {
		this.jOutput.val(this.output);
		this.adjustTextarea(this.jOutput);
	}
};
_.str.uniq = function (a) {
	var r, v = true;
	if (!_.isArray(a)) {
		v = false;
	} else {
		var o = {};
		_.each(a, function (n) {
			if (_.isString(n) || _.isNumber(n)) {
				n = n + '';
				if (n) {
					o[n] = null;
				}
			}
		});
		r = _.keys(o);
	}
	return v && r;
};

////////////////////  ini  ////////////////////
$(function () {
	_.tool.ini();
});
